unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Math;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Timer1: TTimer;
    Edit1: TEdit;
    Panel1: TPanel;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure Heapify (num : integer);
  end;
const
  W=30;
  H=30;
  E=10;
var
  Form1: TForm1;
  N, M, k, s, l:integer;
  arr : array of integer;
  arr2, a3 : array of TPanel;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.Heapify(num: integer);
var a, b, e : integer;
begin
             {
  for e:=0 to N-1 do
  begin
    arr[e].Color:=clYellow;
    arr[step].Color:=clRed;
    arr[step+1].Color:=clRed;
  end;      }

  while (1=1) do
  begin
    a:=2000000000;
    b:=2000000000;
    if (2*num<N+1) then
      a:=arr[2*num];
    if (2*num+1<N+1) then
      b:=arr[2*num+1];
    if (a<=b) and (a<arr[num]) then
    begin
      arr[num]:=arr[2*num]+arr[num];
      arr[num*2]:=arr[num]-arr[num*2];
      arr[num]:=arr[num]-arr[num*2];
      num:=2*num;
    end else if (b<a) and (b<arr[num]) then
    begin
      arr[num]:=arr[2*num+1]+arr[num];
      arr[2*num+1]:=arr[num]-arr[2*num+1];
      arr[num]:=arr[num]-arr[2*num+1];
      num:=2*num+1;
    end else begin
      break;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i, j:integer;
begin
  N:=StrToInt(Edit1.Text);
  setlength(arr, N+1);
  setlength(arr2, N+1);
  Randomize;
  for i := 1 to N do
  begin
    arr[i] := Random(N+10);
    arr2[i].Caption :=inttostr(arr[i]);
  end;
  arr2[0]:=Tpanel.Create(Form1);
  for i := 1 to N do
  begin
    s:=s*s;
    if (i=1) then
    begin
      k:=trunc(log2(i)) ;
      arr2[i]:=TPanel.Create(Form1);
      arr2[i].Width := W;
      arr2[i].Height := H;
      arr2[i].Parent := Form1;
      arr2[i].Left := (Form1.ClientWidth div 2);
      arr2[i].Top := k*10+50+H*k;
      arr2[i].Color:=clYellow;
    end else begin
      k:=trunc(log2(i)) ;
      arr2[i]:=TPanel.Create(Form1);
      arr2[i].Width := W;
      arr2[i].Height := H;
      arr2[i].Parent := Form1;
      if (i mod 2 = 0) then
      begin
        if (i mod 4 = 0) then
        begin
          arr2[i].Left := (arr2[i div 2].Left-((arr2[i div 4].Left-arr2[i div 2].Left) div 2));
        end else begin
          arr2[i].Left := (arr2[i div 2].Left-((-arr2[((i div 2)-1) div 2].Left+arr2[i div 2].Left) div 2));
        end;
        arr2[i].Top := k*10+50+H*k;
        arr2[i].Color:=clYellow;
      end;
      if (i mod 2 <> 0) then
      begin
        arr2[i].Left := (arr2[(i-1) div 2].Left + (arr2[(i-1) div 2].Left-arr2[i-1].Left));
        arr2[i].Top := k*10+50+H*k;
        arr2[i].Color:=clYellow;
      end;
    end;
  end;
  for j:= 1 to N do
    arr2[j].Caption:=IntToStr(arr[j]);
end;



procedure TForm1.Timer1Timer(Sender: TObject);
var i, j : integer;
begin
  for i := N downto 1 do
    Heapify(i);

  M:=N;
  for i:=1 to M do
  begin
    arr[1]:=arr[N]+arr[1];
    arr[N]:=arr[1]-arr[N];
    arr[1]:=arr[1]-arr[N];
    dec(N);
    Heapify(1);
  end;
  N:=M;
  for j:= 1 to N do
    arr2[j].Caption:=IntToStr(arr[j]);
  Timer1.Enabled:=False;

end;

procedure TForm1.Button2Click(Sender: TObject);
var j: integer;
begin
  Timer1.Enabled:=True;
end;

procedure TForm1.Button3Click(Sender: TObject);
var i : integer;
begin
  setlength(a3, N+1);
  for i := 1 to N do
  begin
      l:=trunc(log2(N)) ;
      a3[i]:=TPanel.Create(Form1);
      a3[i].Width := W;
      a3[i].Height := H;
      a3[i].Parent := Form1;
      a3[i].Left := (W+E)*i+100;
      a3[i].Top := l*10+150+H*l;
      a3[i].Color:=clYellow;
      a3[i].Caption:=IntToStr(arr[N-i+1]);
  end;
end;

end.
